Graphviz usage in Python notebooks

Graphviz is a library/program for algorithmic redering of graphs, given their nodes and edges. It can be used to create diagrams programmatically.


In [ ]:
from __future__ import print_function

There are many available Python packages providing APIs for Graphviz. In no particular order:

The VM contains a couple of them: pydot and graphviz

pydot


In [ ]:
import pydot

In [ ]:
# Create a graph and set defaults
dot = pydot.Dot()                                                           
dot.set('rankdir', 'TB')                                                    
dot.set('concentrate', True)                                                
dot.set_node_defaults(shape='record')

In [ ]:
# Add nodes and edges
node = pydot.Node(1, label="FROM")
dot.add_node(node)

node = pydot.Node(2, label="TO")
dot.add_node(node)

dot.add_edge( pydot.Edge(1,2))

Render

To render the graph in the notebook we call the create_svg() method, and the return rendered SVG can be output as the Notebook cell results by using the IPython SVG call


In [ ]:
from IPython.core.display import SVG

img = dot.create_svg()
SVG( data=img )

We can also set properties on the graph nodes


In [ ]:
for n in dot.get_nodes():
    n.set('style', 'filled')
    n.set('fillcolor', 'aliceblue')
    n.set('fontsize', '10')
    n.set('fontname', 'Trebuchet MS, Tahoma, Verdana, Arial, Helvetica, sans-serif')

In [ ]:
SVG( data=dot.create_svg() )

It is also possible to render as PNG and display as image (though the quality, in general, will be lower)


In [ ]:
from IPython.core.display import Image
Image( data=dot.create_png() )

Graphviz

The graphviz package offers a very similar procedure:

  • Create a Graph or DiGraph object
  • Use the node() and edge() methods to add nodes and edges, respectively
  • Render the graph into a file (in the format given in the constructor) by calling the render() method. Available formats will depend on the graphviz installation

In [ ]:
import graphviz as gv

In [ ]:
g1 = gv.Graph(format='svg')
g1.node('A')
g1.node('B')
g1.edge('A', 'B')

Render


In [ ]:
# Render into "example.svg" file
g1.render( filename="example")

Furthermode, the package provides a useful facility for notebooks: Graph or Digraph objects contain a _repr_svg_() method that makes them directly renderizable in a notebook


In [ ]:
# Create a graph
dot = gv.Digraph(comment='The Round Table', engine='dot')

dot.node('A', 'King Arthur', color="blue", fillcolor="lightgray", style="filled", fontcolor="red", fontname="Verdana")
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave', shape="rectangle")

dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false', color="blue")

In [ ]:
# Render in notebook by just outputting the graph as the result of a cell
dot

It is also possible to directly provide a buffer containing a graph written in dot language, by using the Source class:


In [ ]:
src = gv.Source('digraph "countdown" { rankdir=LR; 3 -> 2 -> 1 -> "Go!" }')

In [ ]:
# Again, the result can be rendered directly in the Notebook
src

In [ ]: